<template>
  <qx-table :dbRowClick="rowDblClick" :option="tableOption" :remoteData="remoteData">
    <template v-slot:top>
      <qx-form v-model="searchForm" :option="tableOption" isSearch @submit="startSearch"></qx-form>
    </template>
    <template v-slot:menuLeft>
      <q-btn color="primary" icon="add" unelevated @click="handleCreate">
        {{ $t('Add') }}
      </q-btn>
    </template>
    <template v-slot:menuLeftR>
      <q-icon color="warning" name="mdi-map-marker-question cursor-pointer cursor-hover" size="1.5rem">
        <q-tooltip class="q-pa-md" v-bind="$tooltipAttr">{{ $t('Misc.DblClickCardTips') }}</q-tooltip>
      </q-icon>
    </template>
    <template v-slot:menu="{ row }">
      <q-btn class="col" color="primary" no-wrap outline unelevated @click="handleNotify(row)">
        {{ $t('TaskCore.TaskNotification') }}
      </q-btn>
      <template v-if="row.sysInner !== '1'">
        <q-btn class="col" color="primary" icon="mdi-square-edit-outline" no-wrap outline unelevated
               @click="handleUpdate(row)">
          {{ $t('Edit') }}
        </q-btn>
        <q-btn class="col" color="primary" icon="delete" no-wrap outline unelevated @click="handleDelete(row)">
          {{ $t('Del') }}
        </q-btn>
      </template>
    </template>
  </qx-table>
  <q-modal v-model="modalVisible" :header="formInfo.id ? $t('Edit') : $t('Add')" :width="500" btnAlign="center"
           position="right" @beforeClose="beforeClose" @confirm="submitForm">
    <qx-form ref="FormRef" v-model="formInfo" :option="tableOption"></qx-form>
  </q-modal>
  <q-modal v-model="notifyVisible" v-model:loading="loading" :header="$t('TaskCore.TaskNotification')" :width="500"
           @beforeClose="beforeClose" @confirm="submitForm">
    <qx-form ref="FormRef" v-model="formInfo" :option="notifyOption"></qx-form>
  </q-modal>
  <q-modal v-model="infoVisible" :header="`${$t('Misc.SpecifyDetails')}`" :menuBtn="false" :persistent="false"
           width="620">
    <QInfo :columns="tableOption.column || []" :rowInfo="rowInfo" cols="2" headerWidth="100"></QInfo>
  </q-modal>
</template>
<script setup>
import { i18n, toI18nName } from 'boot/i18n.js'
import { deleteAPI, postAPI, putAPI } from 'src/api/index.js'
import { setRemoteData } from 'src/enum/ajaxRemoteData.js'
import { enableOrDisable, yesOrNo } from 'src/enum/index.js'
import { computed, onMounted, ref } from 'vue'
import { createDialog, deepClone } from 'src/utils/utils.js'

const rowInfo = ref({})
const formInfo = ref({})
const FormRef = ref(null)
const searchForm = ref({})
const loading = ref(false)
const infoVisible = ref(false)
const modalVisible = ref(false)
const notifyVisible = ref(false)
const remoteData = ref({ dicUrl: '/yfagv/api/sdaiAuthApp/list' })
const tableOption = computed(() => {
  return {
    grid: true, span: 22, export: true, labelWidth: 100, searchSpan: 4, headerWidth: 60,
    searchLabelWidth: 60,
    column: [{
      label: `${i18n.t('AppCore.AppCode')}`, field: 'appId', minWidth: 100, required: true, topLeft: true
    }, {
      label: `${i18n.t('AppCore.AppNameENUS')}`, field: 'appNameEnUs', minWidth: 100, search: false,
      hide: true, export: false, showInfo: false
    }, {
      label: `${i18n.t('AppCore.AppName')}`, field: 'appNameZhCn', minWidth: 100, required: true, search: false,
      format: (_, row) => row[toI18nName('appNameZhCn')]
    }, {
      label: 'Token', field: 'apiToken', minWidth: 80, search: false
    }, {
      label: `${i18n.t('BuiltIn')}`, field: 'sysInner', search: false, display: false, options: yesOrNo.value,
      useTag: true, minWidth: 80
    }, {
      label: `${i18n.t('IsEnable')}`, field: 'isEnable', type: 'radio', searchType: 'select', value: '1',
      options: enableOrDisable.value, useTag: true, topRight: true
    }, {
      label: `${i18n.t('CreationTime')}`, field: 'createdTime', display: false, minWidth: 80, search: false
    }, {
      label: `${i18n.t('Remarks')}`, field: 'remark', type: 'textarea', minWidth: 100, search: false, hide: true
    }]
  }
})
const notifyOption = computed(() => {
  return {
    span: 22, labelWidth: 100,
    column: [{
      label: `${i18n.t('TaskCore.TaskNotificationAddress')}`, field: 'appUrl'
    }, {
      label: `${i18n.t('TaskCore.SubscribedEventType')}`, field: 'methods',
      tips: `${i18n.t('TaskCore.SubscribedEventTypeTips')}`
    }]
  }
})

const handleCreate = () => {
  modalVisible.value = true
}
const rowDblClick = row => {
  rowInfo.value = row
  infoVisible.value = true
}
const handleNotify = ({ id: sdaiAuthAppId }) => {
  loading.value = true
  notifyVisible.value = true
  formInfo.value.sdaiAuthAppId = sdaiAuthAppId
  postAPI('/yfagv/api/authAppMethod/query', { sdaiAuthAppId }, data => {
    formInfo.value = data || {}
    loading.value = false
  }, () => {
    loading.value = false
  })
}
const handleUpdate = row => {
  formInfo.value = deepClone(row)
  modalVisible.value = true
}
const handleDelete = row => {
  createDialog({ msg: `${i18n.t('DelTips')}` }, () => {
    deleteAPI(`/yfagv/api/sdaiAuthApp/${row.id}`, {}, () => startSearch('clear'))
  }, () => {})
}
const formatSearchForm = () => ({ ...searchForm.value })
const startSearch = (action, done) => {
  remoteData.value.params = formatSearchForm()
  remoteData.value.clearSelected = true
  if (done) done()
  action === 'clear' && setRemoteData('rcs_app')
}
const beforeClose = () => {
  if (FormRef.value) FormRef.value.onReset()
  modalVisible.value = false
  notifyVisible.value = false
}
const submitForm = mDone => {
  FormRef.value.validate((form, done) => {
    const API = form.id ? putAPI : postAPI
    const uri = form.sdaiAuthAppId ? '/yfagv/api/authAppMethod' : '/yfagv/api/sdaiAuthApp'
    API(uri, form, () => {
      !form.sdaiAuthAppId && startSearch('clear')
      done()
      mDone && mDone()
    }, () => {
      done()
      mDone && mDone(false)
    })
  }, () => mDone && mDone(false))
}
onMounted(startSearch)
</script>